<?php

namespace mpend\modules\share\models;

use Yii;
use mpend\models\User;
use common\models\user\UserBranch;
use common\models\member\Member;
use common\models\spread\SpreadAttract;

/**
 * 会员栏信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SpreadAttractForm extends \common\base\Model {

    /**
     * @var integer 会员用户ID
     */
    public $user_id;

    /**
     * @var integer 会员用户ID
     */
    public $connect_id;

    /**
     * @var string token
     */
    public $token;

    /**
     * @var Member|null 会员模型
     */
    private $_member = null;

    /**
     * @var User|null 用户手机号
     */
    private $_user = null;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'connect_id', 'token'], 'required'],
            [['connect_id', 'user_id'], 'integer'],
            ['user_id', 'validateMember'],
            ['token', 'validateUser'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'mobile' => '手机号',
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateMember($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $this->_member = Member::findAvailable(['user_id' => $this->$attribute])->one();
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateUser($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $token = $this->$attribute;
        if (!$token) {
            return $this->addError($attribute, '请求失败，未授权用户');
        }
        $user = $token->user;
        if ($user && $user->userBranch) {
            return $this->addError($attribute, '当前用户已绑定关系，不用重复执行');
        }
        $this->_user = $user;
    }

    /**
     * 提交绑定
     */
    public function bind() {
        if (!$this->validate()) {
            return null;
        }
        Yii::error([
            $this->_member,
            $this->user_id,
            $this->validate()
        ]);
        $member = $this->_member;
        if (!$member) {
            return null;
        }
        $user = $this->_user;
        if ($user) {
            $branch = new UserBranch();
            $branch->attributes = [
                'user_id' => $user->id,
                'member_id' => $member->id,
                'member_user_id' => $member->user_id,
                'status' => UserBranch::STATUS_ACTIVE,
            ];
            $branch->save();
            return null;
        }

        $spread = SpreadAttract::find()->active()->andWhere([
            'or',
            ['token_id' => $this->token->id],
            ['connect_id' => $this->connect_id],
        ])->one();
        if (!$spread) {
            $spread = new SpreadAttract();
            $spread->attributes = [
                'user_id' => $this->user_id,
                'token_id' => $this->token->id,
                'connect_id' => $this->connect_id,
            ];
        } else {
            if (!$spread->user_id) {
                $spread->user_id = $this->user_id;
            }
            $spread->attributes = [
                'token_id' => $this->token->id,
                'connect_id' => $this->connect_id,
            ];
        }
        if (!$spread->save()) {
            return null;
        }
        return $spread;
    }

}
